package cn.hpclub.server.interceptor;

import cn.hpclub.server.constant.ClientApiConstant;
import cn.hpclub.server.constant.H5ApiConstant;
import cn.hpclub.server.service.DigestService;
import cn.hpclub.server.service.TokenService;
import cn.hpclub.server.util.HttpUtil;
import cn.hpclub.server.util.SystemConfigUtil;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.regex.Pattern;

/**
 * Created by wanglin on 2017/7/19.
 */
public class H5ApiIntercepter implements Interceptor {
    private static Logger log = LoggerFactory.getLogger(H5ApiIntercepter.class);
    private String              AUTH_NO_CHECK = SystemConfigUtil.getSystemConfig().getAuthNoCheck();

    @Override
    public void intercept(Invocation ai) {
        Controller controller = ai.getController();

        String actionMethodName = ai.getMethodName();
        if(isSkipH5AuthFilter(actionMethodName)){
            ai.invoke();
            return;
        }

        String token = controller.getRequest().getHeader(H5ApiConstant.Key.TOKEN);
        String saleType = controller.getRequest().getHeader(H5ApiConstant.Key.SALETYPE);
        if(StrKit.isBlank(saleType) || saleType.equals("1") || saleType.equals("3")){
            // token非法和失效都返回1009，以便客户端统一处理跳到登陆界面。如果有些返回1006，有些返回1009，客户端统一处理非1000都跳到登陆界面，是有问题的，比如只是一般的接口异常。
            if(!TokenService.service.isTokenLegal(token)){
                // token非法
                JSONObject jo = HttpUtil.setResponseObj(ClientApiConstant.ResultCode.SESSION_TIMEOUT,
                        ClientApiConstant.Msg.PARA_ILLEGAL,
                        ClientApiConstant.Msg.PARA_ILLEGAL);
                System.out.println("H5ApiIntercepter isTokenLegal to client msg = " + jo.toString());
                controller.renderText(jo.toString());
                return;
            }
            if(!TokenService.service.isTokenActive(token)){
                // token失效
                JSONObject jo = HttpUtil.setResponseObj(ClientApiConstant.ResultCode.SESSION_TIMEOUT,
                        ClientApiConstant.Msg.MSG_SESSION_TIMEOUT,
                        ClientApiConstant.Msg.MSG_SESSION_TIMEOUT);
                System.out.println("H5ApiIntercepter isTokenActive to client msg = " + jo.toString());
                controller.renderText(jo.toString());
                return;
            }
            // 正常访问
            ai.invoke();
        }
        ai.invoke();
    }

    private boolean isSkipH5AuthFilter(String uri){
        Pattern pattern = null;
        boolean matches = false;
        String[] arrStr = AUTH_NO_CHECK.split("#%#");
        for(String regEx : arrStr){
            pattern = Pattern.compile(regEx);
            matches = pattern.matcher(uri).matches();
            if(matches){
                return matches;
            }
        }
        return matches;
    }
}
